import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o agentsList = [];
    @o firstAgentId = '';
    @o firstAgentName = '';
    @o secondAgentsList = [];
    @o agentName = '';
    @o agentIdentity = '';
    @o agentPhone = '';

    @o secondAgentName = '';
    @o secondAgentIdentity = '';
    @o secondAgentPhone = '';
    @o firstRequest = false;

    will() {

    }

    //返回一级，清空二级数据
    backToFirst() {
        this.firstAgentId = '';
        this.firstAgentName = '';
        this.firstRequest = true;
        this.clearSecondInfo();
    }

    clearSecondInfo() {
        this.secondAgentName = '';
        this.secondAgentIdentity = '';
        this.secondAgentPhone = '';
        this.secondAgentsList = [];
    }


    //输入框，下拉框修改

    agentNameChange(v) {
        if (!this.firstAgentId) {
            this.agentName = v;
        } else {
            this.secondAgentName = v;
        }
    }

    clearHandle() {
        this.getLists(true);
    }

    agentPhoneChange(v) {
        if (!this.firstAgentId) {
            this.agentPhone = v;
        } else {
            this.secondAgentPhone = v;
        }
    }

    agentIdentityChange(v) {
        if (!this.firstAgentId) {
            this.agentIdentity = v;
        } else {
            this.secondAgentIdentity = v;
        }
        this.getLists(true);
    }

    //键盘事件enter
    keydownMethod(e) {
        if (e.which === 13) {
            this.getLists(true);
        }
    }

    //按钮方法

    exportInfo() {
        this.stores.Message.showMessage(3, '暂未开放');
    }


    agentAdd() {
        this.stores.AgentsDialog.fire('agentsDialog:show', {
            agentsList: !!this.firstAgentId ? this.agentsList : [],
            firstAgentName: this.firstAgentName,
            firstAgentId: this.firstAgentId,
        });
    }


    // 获取列表数据
    getAgentsList(loading) {
        loading && this.stores.Frame.showLoading();
        request({
            url: $$.api('getAgentsList'),
            data: {
                phone: this.agentPhone || null,
                name: this.agentName || null,
                label: this.agentIdentity || null
            }
        }).then(res => {
            this.firstRequest = true;
            this.agentsList = res.data;
            this.stores.Frame.hideLoading();
        }).catch(err => {
            this.stores.Message.showMessage(2, err.message);
        });
    }

    getSecondAgentsList(loading) {
        loading && this.stores.Frame.showLoading();
        let id = this.firstAgentId;
        request({
            url: $$.api('getAgentsList'),
            data: {
                phone: this.secondAgentPhone || null,
                name: this.secondAgentName || null,
                label: this.secondAgentIdentity || null,
                parentId: id || null,
            }
        }).then(res => {
            if (id === this.firstAgentId) {
                this.firstRequest = true;
                this.secondAgentsList = res.data;
                this.stores.Frame.hideLoading();
            }
        }).catch(err => {
            this.stores.Frame.hideLoading();
            this.stores.Message.showMessage(2, err.message);
        });
    }

    getLists(loading) {
        if (!this.firstAgentId) {
            this.getAgentsList(loading);
        } else {
            this.getSecondAgentsList(loading);
        }
    }

    //item项操作按钮方法
    editClick(item) {
        this.stores.AgentsDialog.fire('agentsDialog:show', {
            agentsList: !!this.firstAgentId ? this.agentsList : [],
            firstAgentId: this.firstAgentId,
            firstAgentName: this.firstAgentName,
            time: item.time,
            id: item.id
        });
    }

    secondClick(item) {
        this.firstAgentId = item.id;
        this.firstAgentName = item.name;
        this.firstRequest = false;
        this.getLists();
    }

    qrCodeClick(item) {
        let prefix = !!~global.window.location.origin.indexOf('manage-test') ?
            ($$.SEM.STAGING_HREF_ORIGIN + '/') : ($$.SEM.WWW_HREF_ORIGIN + '/');
        this.stores.QrCodeDialog.fire('qrCodeDialog:show', {
            codeUrl: prefix + '?inviterId=' + item.id + '#register',
            downloadName: item.phone
        });
    }

    detailClick(item) {
        this.stores.AgentsDetail.fire('agentsDetail:show', {...item, isSecond: !!this.firstAgentId});
    }

    deleteClick(item) {
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips: '是否要删除该代理商?',
            func: () => {
                this.stores.Frame.showLoading();
                request({
                    url: $$.api('agentsDelete') + item.id,
                    method: 'delete'
                }).then(res => {
                    this.getLists();
                    this.stores.Message.showMessage(1, res.message);
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                });
            }
        })
    }


    mount() {
        this.getLists();
        this.stores.AgentsDialog.on('agentsDialog:update', () => {
            this.getLists(true);
        })
    }

}

export default Store;